//
//  LTTabbar.swift
//  LTWeibo
//
//  Created by mac on 16/6/24.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
protocol LTTabbarDelegate: NSObjectProtocol {


    func addBtnClick()

}

class LTTabbar: UITabBar {
    
    
    weak var addDelegate:LTTabbarDelegate?
    
   override init(frame: CGRect) {
    
       super.init(frame: frame)
    
       setUIView()
    
    
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    
    
   func setUIView() {
    
    addSubview(centerButton)
    
    }
    

   
    

    private lazy var centerButton: UIButton = {
    
        let cButton = UIButton()
        
        cButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        cButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)

        
        cButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        cButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        
        
        //需要设置图片的大小,否则图片可能不显示
        cButton.sizeToFit()
        
        //给按钮添加点击事件
        cButton.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    
        return cButton
    }()
   
    
    
    override func layoutSubviews()
    {
        
        super.layoutSubviews()
        
        let width = UIScreen.mainScreen().bounds.size.width / 5
        
        var index: CGFloat = 0
        
        for value in self.subviews
        {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                
                value.frame.size.width = width
                value.frame.origin.x = index * width
                
                index++
               
                if index == 2
                {
                
                    index++
                }

            }
        }
        
        centerButton.center.x = self.frame.size.width * 0.5
        centerButton.center.y = self.frame.size.height * 0.5
        
        
    }
    
    
    @objc private func btnClick() {
    
        addDelegate?.addBtnClick()
    
    }

   
}
